import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.tree.*;
import javax.swing.border.*;

public class ServerName extends JFrame implements InterInterface,InternalFrameListener
{
	public static int s1 = 0;
	public static int s2 = 0;
	public static int s3 = 0;
	public JPanel pan4 = new JPanel();
	public JPanel pan7 = new JPanel();
	public JPanel pan10 = new JPanel();
	public JTree tree;
	Container con = getContentPane();
	
	public ServerName()
	{
		super("Distributed Service Provider");
		
		con.setLayout(null);
		this.setResizable(false);
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Services");
		
		
		DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Exam Service");
		root.add(parent);
		DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Lab Exam");
		parent.add(child1);
		DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("MCQ Test");
		parent.add(child2);
		
		DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("Vote Service");
		root.add(parent1);
		DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("Voting");
		parent1.add(child11);
		
		
		
		setIconImage(getToolkit().getImage("images/ic.jpg"));
		tree = new JTree(root);
		int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
		int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		tree.addMouseListener(new MouseAdapter(){public void mouseClicked(MouseEvent me){doMouseClicked(me);}});
		JScrollPane jsp = new JScrollPane(tree,v,h);
		
		jsp.setBounds(1,92,160,150);
		
		
		jsp.setBorder(new LineBorder(Color.gray,2));
		con.add(jsp);
		tree.setEnabled(false);
		tree.setBackground(Color.gray);
		la2.setBounds(161,0,570,90);
		
		la2.setBorder(new LineBorder(Color.gray,2));
		con.add(la2);
		
		desktop.setBounds(161,91,570,423);
		desktop.setBorder(new LineBorder(Color.gray,2));
		con.add(desktop);
		
		
		
		pan10.setLayout(null);
		pan10.setBounds(0,0,160,91);
		la.setBounds(0,0,160,91);
		pan10.setBackground(new Color(25,55,96));
		pan10.add(la);
		la.setBorder(new LineBorder(Color.gray,2));
		con.add(pan10);
		
		pan5.setLayout(null);
		
		labe.setBounds(5,5,80,23);
		labe.setFont(new Font("Verdana",Font.PLAIN,13));
		pan5.add(labe);
		taxfield.setBounds(13,29,125,20);
		taxfield.setBorder(new LineBorder(Color.white,1));
		pan5.add(taxfield);
		labe1.setBounds(12,55,80,23);
		labe1.setFont(new Font("Verdana",Font.PLAIN,13));
		pan5.add(labe1);
		jpassfield.setBounds(13,79,125,20);
		jpassfield.setBorder(new LineBorder(Color.white,1));
		pan5.add(jpassfield);
		okbutton.setBounds(78,111,60,20);
		okbutton.setBorder(new LineBorder(Color.white,1));
		pan5.add(okbutton);
		pan5.setBackground(Color.gray);
		pan5.setBounds(1,92,160,150);
		pan5.setBorder(new LineBorder(Color.gray,2));
		pan5.setVisible(false);
		con.add(pan5);
		
		pan4.setLayout(null);
		btLogin.setBounds(27,15,100,20);
		btLogin.setFont(new Font("Verdana",Font.PLAIN,12));
		btLogin.setBorder(new LineBorder(Color.white,1));
		btLogin.setEnabled(true);
		pan4.add(btLogin);
		
		btLogout.setBounds(27,50,100,20);
		btLogout.setFont(new Font("Verdana",Font.PLAIN,12));
		btLogout.setBorder(new LineBorder(Color.white,1));
		btLogout.setEnabled(false);
		pan4.add(btLogout);
		
		btAbout.setBounds(27,85,100,20);
		btAbout.setFont(new Font("Verdana",Font.PLAIN,12));
		btAbout.setBorder(new LineBorder(Color.white,1));
		pan4.add(btAbout);
		
		pan4.setBounds(1,394,160,120);
		pan4.setBorder(new LineBorder(Color.gray,2));
		pan4.setBackground(Color.gray);
		con.add(pan4);
		
		
		jlabel8.setBounds(0,0,570,423);
		jlabel8.setVisible(true);
		//pan8.setBounds(0,0,570,423);
		//pan8.setBackground(new Color(25,55,96));
		desktop.add(jlabel8);
		
		this.setBounds(110,100,740,540);
		//con.setPreferredSize(new Dimension(740,540));
		this.setVisible(true);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		btLogin.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				PrintThread t1 = new PrintThread("thread1");
				t1.start();
				
			}
			
		}
		);
		btLogout.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				btLogin.setEnabled(true);
				btLogout.setEnabled(false);
				tree.setEnabled(false);
				taxfield.setText("");
				jpassfield.setText("");
				
			}
			
		}
		);
		okbutton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String sname = null;
				String spass = null;
				try {
				FileReader fr = new FileReader("lib/us_pass.txt");
				BufferedReader br = new BufferedReader(fr);
				
				sname = br.readLine();
				spass = br.readLine();
				//System.out.println(sname);
				//System.out.println(spass);
				fr.close();
				
				}
				catch(Exception ex){
				}
				
				if (sname.equals(taxfield.getText())&& spass.equals(new String(jpassfield.getPassword())))
				{
				JOptionPane.showMessageDialog(null,"Welcome to the Exam & Vote Server...","Welcome",JOptionPane.INFORMATION_MESSAGE);
				tree.setEnabled(true);
				
				btLogin.setEnabled(false);
				btLogout.setEnabled(true);
				PrintThread1 t1 = new PrintThread1("thread1");
				t1.start();
				}
				else{
				JOptionPane.showMessageDialog(null,"Sorry....Invalid password or user_id.","Information",JOptionPane.INFORMATION_MESSAGE);
				return;
				}			
			}
			
		}
		);
		
		
		try
		{
			UIManager.setLookAndFeel(looks[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
	}
	
	
	private void doMouseClicked(MouseEvent me)
	{
		
		TreePath tp = tree.getPathForLocation(me.getX(),me.getY());
		if(tp != null)
		{
			if((tp.toString()).equals("[Services, Exam Service, Lab Exam]") && s1 == 0)
			{
				TermExam te = new TermExam();
				s1 = 1;
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
				
			}
			
			if((tp.toString()).equals("[Services, Exam Service, MCQ Test]") && s2 == 0)
			{
				MCQTest te = new MCQTest();
				s2 = 1;
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
				
			}
			if((tp.toString()).equals("[Services, Vote Service, Voting]") && s3 == 0)
			{
				Voting te = new Voting();
				s3 = 1;
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
				
			}
			
		}
	}
	
	
   public void internalFrameClosing(InternalFrameEvent e) {
    	
   s1 = 0; 
  s2 = 0; 
  s3 = 0; 
        
    }

    public void internalFrameClosed(InternalFrameEvent e) {
    	
       s1 = 0; 
  s2 = 0; 
  s3 = 0; 
      
           
      }

    public void internalFrameOpened(InternalFrameEvent e) {
    
            
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    	
        
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    	
        
    }

    public void internalFrameActivated(InternalFrameEvent e) {
    	
         
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
         
    }	
    public static void main(String args[])
	{
		ServerName sr = new ServerName();
		
	}
}


